<?php

defined('BY_JYA') or exit('error');
/*
	游戏营销
*/
$tabs=array(
	array('op'=>'list','title'=>'游戏活动列表'),
	array('op'=>'edit','title'=>'添加/编辑游戏活动'),
);
set_html('tabs',$tabs);
$_RQ['op']=$_RQ['op']?:'list';
$return['types']=array(
		'1'=>'订单满额参与',
		'2'=>'购买指定商品',
		'3'=>'新用户参与',
	);
if($_RQ['op']=='list'){
	$params=array('uniacid'=>$_SESSION['uniacid']);
	
	if(isset($_RQ['status'])){
		$params['status']=intval($_RQ['status']);
		$return['params']['status']=$_RQ['status'];
	}
	if($_RQ['title']){
		$params['title like']=trim($_RQ['title']);
		$return['params']['title']=$_RQ['title'];
	}
	$parameter=array(
		'params'=>$params,
		'limit'=>array(intval($_RQ['page'])?:1,10),
	);
	$list=$this->get_list('game',$parameter,$total);
	if($list){
		foreach($list as &$item){
			$item['game_title']=pdo_getcolumn('jy_h5game_reply',array('id'=>$item['game_id']),'title');
			
		}
	}
	$return['list']=$list;
	$totalpage=ceil($total/$psize);
	$totalpage=intval($totalpage);
	$return['totalpage'] =$totalpage;
	$return['params']['page'] =$pindex;
	$htmls=array(
		'type'=>'list',
		'list'=>array('data'=>'list','tip'=>'[<red>温馨提示</red>]：绑定游戏后，该游戏就无法正常参与，只有满足条件的才能参与（比如消费一次，有一次参与名额！）！','list'=>array(
			array('name'=>'title','title'=>'活动标题','type'=>'string'),
			array('name'=>'type','title'=>'参与条件','type'=>'radio','options'=>'types'),
			array('name'=>'game_title','title'=>'绑定游戏','type'=>'string'),
			array('name'=>'status','title'=>'活动状态','type'=>'switch','set'=>'set'),
			array('name'=>'createtime','title'=>'创建时间','type'=>'time'),
		)),
		'search'=>array(
			array('type'=>'radio','width'=>15,'name'=>'status','title'=>'活动状态','options'=>'statuss'),
			array('type'=>'string','width'=>15,'name'=>'title','title'=>'活动标题'),
		),
	);
	
	$return['htmls']=$htmls;
	$return['statuss']=array(
		'0'=>'关闭',
		'1'=>'启用',
	);
	exi($return);
	
}
if($_RQ['op']=='set'){
	$id=intval($_RQ['id']);
	$post=get_postdata();
	$status=intval($post['status']);
	$this->pdo_update('game',array('status'=>$status),array('id'=>$id));
	if($status==0){
		exi('关闭成功！');
	}else{
		exi('开启成功！');
	}
}
if($_RQ['op']=='edit'){
	if($_RQ['postdata']){
		$post=get_postdata();
		$data=array(
			'title'=>trim($post['title']),
			'game_id'=>intval($post['game_id']),
			'type'=>intval($post['type']),
			'price'=>round($post['price'],2),
			'goodsid'=>intval($post['goodsid']),
			'time'=>intval($post['time']),
			'status'=>intval($post['status']),
			'sendtitle'=>trim($post['sendtitle']),
			'sendimage'=>trim($post['sendimage']),
			'senddesc'=>trim($post['senddesc']),
		);
		$id=intval($post['id']);
		if($id){
			$this->pdo_update('game',$data,array('id'=>$id,'uniacid'=>$_SESSION['uniacid']));
		}else{
			$data['uniacid']=$_SESSION['uniacid'];
			$data['createtime']=TIMESTAMP;
			$this->pdo_insert('game',$data);
		}
		exi('操作成功！','','list');
	}
	$id=intval($_RQ['id']);
	if($id){
		$item=$this->pdo_get('game',array('id'=>$id));
	}else{
		$item=array('title'=>'','content'=>array('goodsid'=>''));
	}
	$item['sendtitle']=$item['sendtitle']?$item['sendtitle']:'[nickname]:恭喜您，获得一个参与奖励活动机会';
	$item['sendimage']=$item['sendimage']?$item['sendimage']:'../core/resource/images/h5game/bigwheel.png';
	$item['senddesc']=$item['senddesc']?$item['senddesc']:'活动有时间限制，请及时参与！';

	$return['item']=$item;
	$htmls=array(
		'type'=>'edit',
		'edit'=>array(
			array('name'=>'title','title'=>'活动标题','type'=>'string'),
			array('name'=>'game_id','title'=>'绑定游戏','type'=>'select','options'=>'games','help'=>'这里的游戏来自<red>互动营销活动</red>，只有已发布的，而且还在活动中的才能选择，可以先到:站点宣传--><red>互动营销活动</red>中创建游戏活动，然后这里再绑定'),
			array('name'=>'type','title'=>'参与条件','type'=>'radio','options'=>'types'),
			array('name'=>'goodsid','title'=>'选择商品','type'=>'choose','help'=>'','action'=>'goods.get_goods/','show'=>array('type'=>2)),
			array('name'=>'price','title'=>'最低订单金额','type'=>'string','help'=>'','show'=>array('type'=>1)),
			array('name'=>'time','title'=>'参与时机','type'=>'radio','options'=>'times','show'=>array('type !='=>3)),
			array('type'=>'string','name'=>'sendtitle','title'=>'推送标题','help'=>'变量 [nickname] 会员昵称'),
			array('type'=>'image','name'=>'sendimage','title'=>'推送封面'),
			array('type'=>'string','name'=>'senddesc','title'=>'推送说明','help'=>'变量 [nickname] 会员昵称'),	
			array('name'=>'status','title'=>'是否开启','type'=>'switch'),
		),
	);
	
	$return['htmls']=$htmls;
	$return['times']=array(
		'1'=>'订单完成时',
		'2'=>'订单付款时',
	);
	$return['games']=pdo_getall('jy_h5game_reply',array('endtime >'=>time(),'status'=>2,'uniacid'=>$_SESSION['uniacid']),array('id','title'));
	exi($return);
}
if($_RQ['op']=='del'){
	model('core/php_ctrl')->del(array(
		'tablename'=>$this->tablename('game'),
	));
}
